package controllers

import (
	"github.com/gin-gonic/gin"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/models"
	"go.didapinche.com/foundation/apisix-dashboard/internal/app/services"
	"net/http"
)

type ConsumerController struct {
	service services.ConsumerService
}

func NewConsumerController(service services.ConsumerService) *ConsumerController {
	return &ConsumerController{service: service}
}

func (ctl ConsumerController) Get(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	u, err := ctl.service.Get(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Get failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl ConsumerController) List(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	name := c.Query("name")
	us, err := ctl.service.List(name, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService List failed:%v", err)
	}
	c.JSON(http.StatusOK, us)
}

func (ctl ConsumerController) Create(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(models.Consumer)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	param.Username = param.ID
	err := ctl.service.Create(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Create failed:%v", err)
	}
}
func (ctl ConsumerController) Update(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	param := new(models.Consumer)
	if err := c.ShouldBind(param); err != nil {
		c.String(http.StatusBadRequest, "bind params error:%v", err)
		return
	}
	param.Username = param.ID
	err := ctl.service.Update(param, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Update failed:%v", err)
	}
}

func (ctl ConsumerController) Delete(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	key := c.Param("id")
	err := ctl.service.Delete(key, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerService Delete failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}

func (ctl ConsumerController) GetName(c *gin.Context) {
	env, exist := c.Get("env")
	if !exist {
		c.String(http.StatusForbidden, "env not exist")
	}
	name := c.Query("name")
	u, err := ctl.service.GetName(name, env.(string))
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerController GetName failed:%v", err)
	}
	c.JSON(http.StatusOK, u)
}

func (ctl ConsumerController) Syn(c *gin.Context) {
	param := new(struct {
		Consumer *models.Consumer `json:"consumer"`
		Env      []string         `json:"env"`
	})
	err := ctl.service.Syn(param.Consumer, param.Env)
	if err != nil {
		c.String(http.StatusBadRequest, "ConsumerController Syn failed:%v", err)
	}
	c.JSON(http.StatusOK, "")
}
